package com.yuanzi.base.auth;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.yuanzi.base.constants.AuthConstants;
import com.yuanzi.base.redis.OuterHashRedisService;

/**
 * 保存成员对课程的关系
 * 1:参与编辑，2:学校授权。3:创建者
 * @author cui
 * @data 2017年9月26日
 *
 */
@Service
public class AuthMemberCourseRedisService {
	
	private static String serviceName = AuthConstants.AUTH_SERVICE_NAME;
	
	/*--------------存储教师对课程的权限：Hash形式：zuul:course_right:teacherMemberId(key):courseVersionId(field)=>权限逗号分割(value)---------------*/	
	/**
	 * 获取成员对课程的权限
	 * @author cui
	 * @param courseVersionId
	 * @param memberId
	 * @return
	 */
	public static String getRightOfMemberOnCourse(String memberId, String courseVersionId){
		return OuterHashRedisService.hget(serviceName, AuthConstants.REDIS_COURSE_RIGHT+memberId, courseVersionId);
	}
	
	/**
	 * 设置成员对课程的权限
	 * @param memberId
	 * @param courseVersionId
	 * @param right
	 */
	public static void setRightOfMemberOnCourse(Long memberId, Long courseVersionId, String right){
		OuterHashRedisService.hset(serviceName, AuthConstants.REDIS_COURSE_RIGHT+memberId.toString(), courseVersionId.toString(), right);
	}
	
	/**
	 * 批量设置设置成员对课程的权限(覆盖)
	 * @author cui
	 * @param memberId
	 * @param courseRightMap
	 */
	public static void setRightOfMemberOnCourseInBatch(Long memberId, Map<String, String> courseRightMap){
		OuterHashRedisService.hmset(serviceName, AuthConstants.REDIS_COURSE_RIGHT+memberId.toString(), courseRightMap);
	}
	
	/**
	 * 删除成员对特定课程的权限
	 * @author cui
	 * @param courseVersionId
	 * @param memberId
	 */
	public static void delRightOfMemberOnCourse(Long courseVersionId, Long memberId){
		OuterHashRedisService.hdel(serviceName, AuthConstants.REDIS_COURSE_RIGHT+memberId.toString(), courseVersionId.toString());
	}
	
	/**
	 * 删除成员对所有课程权限redis
	 * @author cui
	 * @param memberId
	 */
	public static void delRightOfMemberOnCourseByMemberId(Long memberId){
		OuterHashRedisService.del(serviceName, AuthConstants.REDIS_COURSE_RIGHT+memberId.toString());
	}
	
	/**
	 * 批量删除成员对特定课程权限
	 * @author cui
	 * @param courseVersionId
	 * @param memberIds
	 */
	public static void delRightOfMemberOnCourseInCourse(String courseVersionId, String... memberIds){
		if(memberIds.length == 0){
			return ;
		}
		for(String memberId : memberIds){
			OuterHashRedisService.hdel(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId, courseVersionId, memberId);
		}
	}
	
	/**
	 * 没有权限时设置默认值
	 * @param memberId
	 * @param courseVersionId
	 */
	public static void setNoneRightOfMemberOnCourse(String memberId, String courseVersionId){
		OuterHashRedisService.hset(serviceName, AuthConstants.REDIS_COURSE_RIGHT+memberId, courseVersionId, AuthConstants.COURSE_RIGHT_NONE);
	}
}
